/***************************************************
 ** @Desc : This file for 用户表
 ** @Time : 2018.12.28 15:45 
 ** @Author : Joker
 ** @File : users
 ** @Last Modified by : Joker
 ** @Last Modified time: 2018.12.28 15:45
 ** @Software: GoLand
****************************************************/
package models

import (
	"github.com/astaxie/beego/orm"
	"sub_account/utils"
)

type Users struct {
	Id        int
	UserName  string
	LoginName string
	LoginPwd  string
	Salt      string
	EditTime  string
	Version   int
}

func (*Users) TableEngine() string {
	return "INNODB"
}

func (*Users) TableName() string {
	return UsersTBName()
}

//判断当前登录用户是否存在
func (*Users) UserIsExit(uName string) bool {
	om := orm.NewOrm()
	return om.QueryTable(UsersTBName()).Filter("login_name", uName).Exist()
}

/* *
 * @Author: Joker
 * @Description: 添加用户信息
 * @Date: 2018-12-28 14:38:17
 * @Param: Users
 * @return: int: 判断成功的标识
 * @return: int64:	在表中哪一行插入
 **/
func (*Users) InsertUsers(sr Users) (int, int64) {
	om := orm.NewOrm()
	in, _ := om.QueryTable(UsersTBName()).PrepareInsert()
	id, err := in.Insert(&sr)
	if err != nil {
		utils.LogError("InsertUsers failed to insert for: ", err)
		return utils.FAILED_FLAG, id
	}
	return utils.SUCCESS_FLAG, id
}

/*读取单个用户信息*/
func (*Users) SelectOneUser(login_name string) (conf Users) {
	om := orm.NewOrm()
	err := om.QueryTable(UsersTBName()).Filter("login_name", login_name).One(&conf)
	if err != nil {
		utils.LogError("SelectOneUser failed to select user: ", err)
	}
	return conf
}
